<?php

namespace App\Jobs;

use App\Services\GitService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class GitJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $project;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($project)
    {
        $this->project = $project;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $git    = new GitService($this->project);
        $files  = $git->getChangedFiles();
        $path   = $git::getRootPath($this->project);
        // 2.将文件存储在队列中
        foreach ($files as $type => $fileArr) {
            foreach( $fileArr as $file) {
                FileJob::dispatch($type, $file, $path);
            }
        }
    }
}
